﻿using System.Drawing;
using Com.OpenCode.Framework.Core;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;

namespace Com.OpenCode.Framework.DevForm
{
    public class RibbonSubButtonUI : BarSubItem, IMenuVisible
    {
        private BarItemVisibility _Visibility = BarItemVisibility.Always;

        #region 构造函数

        public RibbonSubButtonUI(BuildItemEventArgs args)
        {
            args.Plugin.Owner = this;
            PaintStyle = BarItemPaintStyle.CaptionGlyph;
            RibbonStyle = RibbonItemStyles.Large;
            Caption = args.Plugin.Properties.Get("Text").ToString();
            Image image = null;
            if (args.Plugin.Properties.Exist("Image"))
            {
                string imagePath = System.IO.Path.Combine(args.Plugin.Addin.FileInfo.DirectoryName, args.Plugin.Properties.GetString("Image"));
                if (System.IO.File.Exists(imagePath))
                {
                    image = Image.FromFile(imagePath);
                }
            }
            else
            {
                image = IconDoozer.BulidItem(args.Plugin.Addin, args.Plugin.Properties.GetString("ImageId"));
            }
            if (image == null)
            {
                image = DevFormImage.Ribbon32;
            }
            LargeGlyph = image;
            Glyph = image;
            (args.Owner as RibbonPageGroupUI).ItemLinks.Add(this);
            (args.Owner as RibbonPageGroupUI).Children.Add(this);
        }

        #endregion

        public void CheckVisible()
        {
            _Visibility = BarItemVisibility.Never;
            for (int i = 0; i < ItemLinks.Count; i++)
            {
                if (ItemLinks[i].Visible)
                {
                    _Visibility = BarItemVisibility.Always;
                }
            }
        }
    }
}